import * as Cesium from "cesium"
import { DrawerBase, EventsConfig } from "./DrawerBase"
import * as CoordinatesTool from "../../../utils/CoordinatesTool"
import { generateSameLatitudeCurve } from "../../../utils"

/**
 * @author Oo青花鱼
 * @description 矩形绘制
 * @example
 * //实例化Drawer
 * const drawer = new RectangleDrawer(viewer);
 * drawer.start() //开始绘制
 * drawer.end() //结束绘制
 * drawer.clear() //清空绘制的图形
 * drawer.destroy() //销毁实例
 * drawer.data //访问绘制的Entity
 * drawer.onClose = (entity:Cesium.Entity)=>{
 *    console.log('绘制了一个图形',entity)
 * }
 */
export class RectangleDrawer extends DrawerBase {
  protected helpers: Cesium.Entity.ConstructorOptions[] = [
    //鼠标位置
    {
      position: new Cesium.CallbackPositionProperty(() => {
        return this.mousePosition
      }, false),
      point: {},
    },
    //矩形
    {
      polyline: {
        clampToGround: true,
        positions: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length === 1) {
            const rectangle = Cesium.Rectangle.fromCartesianArray([
              this.featurePositions.value[0],
              this.mousePosition,
            ])
            const northwest = CoordinatesTool.cartographicToCartesian3(
              Cesium.Rectangle.northwest(rectangle),
            ) // 西北角
            const northeast = CoordinatesTool.cartographicToCartesian3(
              Cesium.Rectangle.northeast(rectangle),
            ) // 东北角
            const southwest = CoordinatesTool.cartographicToCartesian3(
              Cesium.Rectangle.southwest(rectangle),
            ) // 西南角
            const southeast = CoordinatesTool.cartographicToCartesian3(
              Cesium.Rectangle.southeast(rectangle),
            ) // 东南角

            return [
              ...generateSameLatitudeCurve(northwest, northeast),
              ...generateSameLatitudeCurve(southeast, southwest),
              northwest,
            ]
          } else return undefined
        }, false),
      },
      rectangle: {
        coordinates: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length === 1) {
            return Cesium.Rectangle.fromCartesianArray([
              this.featurePositions.value[0],
              this.mousePosition,
            ])
          } else return undefined
        }, false),
      },
    },
  ]
  protected events: EventsConfig = {
    cesiumEvents: [
      //左键点击事件
      {
        type: "click",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          //拾取坐标
          const position = this.scene.pickPosition(e.position)
          position && this.featurePositions.value.push(position)

          if (this.featurePositions.value.length === 2) {
            //根据顶点绘制出图形
            this.drawFeature({
              rectangle: {
                coordinates: Cesium.Rectangle.fromCartesianArray([
                  ...this.featurePositions.value,
                ]),
              },
            })

            //初始化折线绘制状态
            this.featurePositions.value = []
          }
        },
      },
    ],
  }
}
